﻿using DLL;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.FallNursingMeasuresToPrevent
{
    public partial class AddFallNursingMeasuresToPrevent : Form
    {
        #region 变量声明
        public NursingFormSystem.NurFormsService.NursingForm formService = new NursingFormSystem.NurFormsService.NursingForm();
        NursingFormRecord record = new NursingFormRecord();
        Tb_User LoginUser;
        PatientInfo Patientinfo;
        string Id = string.Empty;
        #endregion

        #region 窗体构造函数
        public AddFallNursingMeasuresToPrevent(Tb_User loginUser, PatientInfo patientInfo, string id)
        {
            InitializeComponent();
            //成员变量赋值
            Patientinfo = patientInfo;
            LoginUser = loginUser;
            this.Id = id;
            //绑定数据
            BindInfo(Id);
        }
        public AddFallNursingMeasuresToPrevent(string[] loginUser, string[] patientInfo)
        {
            InitializeComponent();
            //成员变量赋值
            Patientinfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            LoginUser = ArrayToObject.ArrayToLoginUser(loginUser);
            this.Id = string.Empty;
            //绑定数据
            BindInfo(this.Id);
        }
        public AddFallNursingMeasuresToPrevent(string[] loginUser, string[] patientInfo, string id)
        {
            InitializeComponent();
            //成员变量赋值
            Patientinfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            LoginUser = ArrayToObject.ArrayToLoginUser(loginUser);
            this.Id = id;
            //绑定数据
            BindInfo(Id);
        }
        #endregion

        #region 显示赋值
        public void BindInfo(string Id)
        {
            dateEditpingguriqi.DateTime = DateTime.Now;     //默认显示当前日期
            if (string.IsNullOrEmpty(Id)) return;
            string sql = string.Format("SELECT TOP 1 NursingFormData FROM NursingFormRecord(NOLOCK) WHERE Id='{0}'", Id);
            DataSet ds = SqlHelper.ExecuteDataset(sql, System.Data.CommandType.Text);
            if (ds == null) return;
            if (ds.Tables.Count <= 0) return;
            if (ds.Tables[0].Rows.Count <= 0) return;
            DataTable tab = ds.Tables[0];
            var NursingFormData = Convert.ToString(tab.Rows[0]["NursingFormData"]);
            FallMTP fmtp = new FallMTP();
            fmtp = XmlUtil.XMLToObject<FallMTP>(NursingFormData, Encoding.UTF8);
            dateEditpingguriqi.DateTime = Convert.ToDateTime(fmtp.RiQi);    //日期
            textdiedaopingfen.Text = fmtp.DieDaoPingFen;     //跌倒评分
            #region 护理措施
            if (string.IsNullOrEmpty(fmtp.HuLiCuoShi)) return;
            checkhulicuoshi1.Checked = fmtp.HuLiCuoShi.Split('#')[0].Equals("1");
            checkhulicuoshi2.Checked = fmtp.HuLiCuoShi.Split('#')[1].Equals("1");
            checkhulicuoshi3.Checked = fmtp.HuLiCuoShi.Split('#')[2].Equals("1");
            checkhulicuoshi4.Checked = fmtp.HuLiCuoShi.Split('#')[3].Equals("1");
            checkhulicuoshi5.Checked = fmtp.HuLiCuoShi.Split('#')[4].Equals("1");
            checkhulicuoshi6.Checked = fmtp.HuLiCuoShi.Split('#')[5].Equals("1");
            checkhulicuoshi7.Checked = fmtp.HuLiCuoShi.Split('#')[6].Equals("1");
            checkhulicuoshi8.Checked = fmtp.HuLiCuoShi.Split('#')[7].Equals("1");
            checkhulicuoshi9.Checked = fmtp.HuLiCuoShi.Split('#')[8].Equals("1");
            checkhulicuoshi10.Checked = fmtp.HuLiCuoShi.Split('#')[9].Equals("1");
            checkhulicuoshi11.Checked = fmtp.HuLiCuoShi.Split('#')[10].Equals("1");
            checkhulicuoshi12.Checked = fmtp.HuLiCuoShi.Split('#')[11].Equals("1");
            checkhulicuoshi13.Checked = fmtp.HuLiCuoShi.Split('#')[12].Equals("1");
            checkhulicuoshi14.Checked = fmtp.HuLiCuoShi.Split('#')[13].Equals("1");
            checkhulicuoshi15.Checked = fmtp.HuLiCuoShi.Split('#')[14].Equals("1");
            checkhulicuoshi16.Checked = fmtp.HuLiCuoShi.Split('#')[15].Equals("1");
            checkhulicuoshi17.Checked = fmtp.HuLiCuoShi.Split('#')[16].Equals("1");
            checkhulicuoshi18.Checked = fmtp.HuLiCuoShi.Split('#')[17].Equals("1");
            checkhulicuoshi19.Checked = fmtp.HuLiCuoShi.Split('#')[18].Equals("1");
            checkhulicuoshi20.Checked = fmtp.HuLiCuoShi.Split('#')[19].Equals("1");
            #endregion
        }
        #endregion

        #region 保存
        private void simpleButtonbaocun_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(dateEditpingguriqi.Text))
                dateEditpingguriqi.DateTime = DateTime.Now;

            FallMTP fmtp = new FallMTP();
            fmtp.RiQi = dateEditpingguriqi.Text + " " + DateTime.Now.ToString("HH:mm:ss");      //评估日期
            fmtp.DieDaoPingFen = textdiedaopingfen.Text;      //跌倒评分
            fmtp.HuLiCuoShi = (checkhulicuoshi1.Checked ? 1 : 0) + "#" + (checkhulicuoshi2.Checked ? 1 : 0) + "#" +
                              (checkhulicuoshi3.Checked ? 1 : 0) + "#" + (checkhulicuoshi4.Checked ? 1 : 0) + "#" +
                              (checkhulicuoshi5.Checked ? 1 : 0) + "#" + (checkhulicuoshi6.Checked ? 1 : 0) + "#" +
                              (checkhulicuoshi7.Checked ? 1 : 0) + "#" + (checkhulicuoshi8.Checked ? 1 : 0) + "#" +
                              (checkhulicuoshi9.Checked ? 1 : 0) + "#" + (checkhulicuoshi10.Checked ? 1 : 0) + "#" +
                              (checkhulicuoshi11.Checked ? 1 : 0) + "#" + (checkhulicuoshi12.Checked ? 1 : 0) + "#" +
                              (checkhulicuoshi13.Checked ? 1 : 0) + "#" + (checkhulicuoshi14.Checked ? 1 : 0) + "#" +
                              (checkhulicuoshi15.Checked ? 1 : 0) + "#" + (checkhulicuoshi16.Checked ? 1 : 0) + "#" +
                              (checkhulicuoshi17.Checked ? 1 : 0) + "#" + (checkhulicuoshi18.Checked ? 1 : 0) + "#" +
                              (checkhulicuoshi19.Checked ? 1 : 0) + "#" + (checkhulicuoshi20.Checked ? 1 : 0);      //护理措施
            fmtp.FormId = (int)FormType.FallMTP;     //表单ID
            fmtp.FormName = EnumHelper.GetDescription(FormType.FallMTP);      //表单名字
            record.FormId = (int)FormType.FallMTP;     //表单ID
            record.FormName = EnumHelper.GetDescription(FormType.FallMTP);     //表单名字
            record.PatientId = Patientinfo.PATIENT_ID;     //住院号
            record.VisitId = Patientinfo.VISIT_ID;     //住院次数
            record.PatientName = Patientinfo.PATIENT_NAME;     //患者姓名
            record.BedNo = Patientinfo.BED_NO;     //床号
            record.DepartmentId = Patientinfo.DEPARTMENT_ID;    //当前患者科室ID
            record.DepartmentName = Patientinfo.DEPARTMENT_NAME;    //当前患者科室名字
            record.AreaId = Patientinfo.NURSE_CELL_CODE;     //当前患者病区ID
            record.AreaName = Patientinfo.NURSE_CELL_NAME;    //当前患者病区名字
            record.AssessmentDate = Convert.ToDateTime(fmtp.RiQi);    //日期时间
            record.Id = Id == "" ? 0 : Convert.ToInt32(Id);
            record.NursingFormData = XmlUtil.ObjectToXML(fmtp, Encoding.UTF8);
            var ret = formService.SetNursingFormRecord(Newtonsoft.Json.JsonConvert.SerializeObject(record));
            if (ret.Code == 1)
            {
                MessageBox.Show(ret.M);
                this.DialogResult = DialogResult.OK;
            }

        }
        #endregion

        #region 取消
        private void simpleButtonquxiao_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
        #endregion

        #region 设置 enter键当做Tab切换
        /// <summary>
        /// 设置 enter键当做Tab切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TemperatureEdit1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }
        #endregion
    }
}
